home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 239 < prev    next >
Encoding:
Text File  |  1996-08-06  |  7.9 KB  |  193 lines

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de>
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Are all Windows programs ill-formed?
  5. Date: 2 Feb 1996 15:49:09 GMT
  6. Organization: ?
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <9602021032.AA05302@lts.sel.alcatel.de>
  9. References: <AE5J83na99@qsar.chem.msu.su> <9602010236.26117@mulga.cs.mu.OZ.AU>
  10. NNTP-Posting-Host: taumet.eng.sun.com
  11. Content-Type: text
  12. In-Reply-To: fjh@munta.cs.mu.OZ.AU's message of 01 Feb 1996 09:07:11 PST
  13. Content-Length: 7291
  14. X-Lines: 171
  15. Originator: clamage@taumet
  16.  
  17. In article <9602010236.26117@mulga.cs.mu.OZ.AU> fjh@munta.cs.mu.OZ.AU
  18. (Fergus Henderson) writes:
  19.  
  20. |> "Eugene Radchenko" <eugene@qsar.chem.msu.su> writes:
  21.  
  22. |> >I have just realized that something is grossly overlooked in C++ standard.
  23.  
  24. |> I think the problem is due to Microsoft overlooking the C and C++
  25. |> standards, not vice versa.
  26.  
  27. |> >It requires every program to have the main() function (which implementation
  28. |> >is not allowed to supply) with parameters (void) or (int argc, int*argv[]).
  29.  
  30. |> The implementation is allowed to make the program work even if main() is
  31. |> not defined, so long as it issues a diagnostic.
  32.  
  33. By a curious coincidence, this point has just been discussed in
  34. comp.std.c.  The concensus of the experts there was that the absense
  35. of main (or a non-conformant main) is simply undefined behavior: no
  36. diagnostic required.
  37.  
  38. |> >However, Windows programs have another convention: prrogram must contain
  39. |> >the function
  40. |> >  int WinMain(HINSTANCE curInst, HINSTANCE prevInst, LPSTR cmdline, int cmdShow)
  41.  
  42. I believe that the simple answer is that Windows programs operate in a
  43. unhosted environment.  In an unhosted evironment, the language is
  44. defined, but which libraries are present, how the program is started,
  45. etc., etc., are implementation defined.
  46.  
  47. By definition, a program written for an unhosted environment is not
  48. portable outside that environment.  And in fact, a program written for
  49. Windows will probably not work on my Unix box:-).
  50.  
  51. Another interesting point that was made in the discussion on
  52. comp.std.c was that g++ normally operates in an unhosted environment,
  53. at least according FSF.  It just so happens that the implementation
  54. defined rules are *very* close to what the standard requires of a
  55. hosted environment.  (The machine I'm typing this on runs Sun OS 4.1.
  56. I have two compilers available, g++ and Sun CC.  On this machine, at
  57. least, the unhosted g++ is closer to the standard than the supposedly
  58. hosted environment of Sun CC:-).  As I understand it, the reason g++
  59. is unhosted is that they don't provide the libraries themselves, and
  60. thus cannot make any guarantees about them.)
  61.  
  62. |> A compiler is allowed to accept the program
  63.  
  64. |>     int WinMain(HINSTANCE curInst, HINSTANCE prevInst, LPSTR cmdline,
  65. |>         int cmdShow)
  66. |>     {
  67. |>         return 0;
  68. |>     }
  69.  
  70. |> so long as it issues at least one diagnostic, e.g.
  71. |> "warning: standard C++ requires a definition of main()".
  72.  
  73. In conformant mode, I would expect to see something along the lines of
  74. `_main undefined' when linking, although I am far from sure that the
  75. standard requires it.
  76.  
  77. I feel fairly sure that Microsoft does provide a conformant mode, and
  78. that when compiling in this mode, you will get such an error message.
  79. Of course, when compiling in this mode, you probably cannot use
  80. Windows.  But the standard does not require the capability to use
  81. Windows, so this is not a problem.
  82.  
  83. |> But if a compiler does not accept
  84.  
  85. |>     int main() { return 0; }
  86.  
  87. |> as a complete program, then that compiler is not a conforming C or C++
  88. |> implementation.
  89.  
  90. Most compilers have non-conformant modes, with extensions.  I'm not
  91. familiar with the newer Microsoft products (my MS C compiler is
  92. version 1.2), but I rather suspect that if you don't request Windows
  93. support (which is not really needed in the above program), the
  94. compiler and linker will accept it.
  95.  
  96. Windows support is an extension, and the compiler is entitled to make
  97. any requirements it wishes on programs using the extension.  A program
  98. using the extension is not a C program, or in this case, since the
  99. extension really involves the environment, and not the language
  100. itself, it is not a C program for a hosted environment.
  101.  
  102. Whether requiring a different start-up procedure than calling main is
  103. a good idea for this extension can be discussed under quality of
  104. implementation; it is not a standards question, however.
  105.  
  106. |> >Some of this is of course quirks or even outright ignored in Win32
  107. |> >(prevInst), but the full non-parsed command line could be useful sometimes,
  108. |> >and not all implementations provide the function retrieving it.
  109.  
  110. |> In many operating systems, the command-line parsing is done by the
  111. |> shell, not the OS.  Such operating systems cannot provide the non-parsed
  112. |> command line.
  113.  
  114. |> >And anyway this is definitely not a main() from the specs.
  115. |> >Maybe the requirements on the main() should be relaxed somehow?
  116.  
  117. |> Are you suggesting relaxing the requirements on implementations,
  118. |> or the requirements on programs?
  119.  
  120. |> I presume you are referring to 3.6.1[basic.start.main]/1:
  121.  
  122. |> |    A program shall contain a global function called main, which is the
  123. |> |    designated start of the program.
  124.  
  125. |> You are correct to infer that this means that all Windows programs
  126. |> which do not define main() are ill-formed.
  127.  
  128. No, he's not.  They are simply programs for non-hosted environments.
  129.  
  130. I personally think that the Microsoft environment stinks, but I still
  131. get the feeling that there is a double standard at work here.  If
  132. someone complains that g++ doesn't issue a diagnostic when I define a
  133. nested function, dozens of people (including you, Fergus?) will
  134. quickly point out that all I have to do is add the flags -ansi
  135. -pedantic, and it will; without these flags, the compiler is operating
  136. in a non-conformant mode, and this fact is in the compiler
  137. documentation.  Unless things have changed in the last 4 years, you
  138. need to give a special flag to the compiler to get Windows support.
  139. The default mode was the conformant mode.  And the non-conformity is
  140. actually simply an unhosted environment (where it is conformant).
  141.  
  142. |> However, that means only that a compiler is required to issue
  143. |> a diagnostic; it does not mean that the compiler must reject the
  144. |> program.
  145.  
  146. |> Obviously a program that does not define main() is not going to be
  147. |> portable, so I don't think that requirement on programs should be
  148. |> relaxed.
  149.  
  150. |> Windows compilers which wish to conform to the C and C++ standards
  151. |> should include code similar to
  152.  
  153. |>     HINSTANCE __curInst;
  154. |>     HINSTANCE __prevInst;
  155. |>     LPSTR __cmdline;
  156. |>     int WinMain(HINSTANCE curInst, HINSTANCE prevInst, LPSTR cmdline,
  157. |>         int cmdShow)
  158. |>     {
  159. |>         int argc;
  160. |>         char **argv;
  161.  
  162. |>         __curInst = curInst;
  163. |>         __prevInst = prevInst;
  164. |>         __cmdLind = cmdLine;
  165. |>         __parse_command_line(cmdline, &argc, &argv);
  166. |>         return main(argc, argv);
  167. |>     }
  168.  
  169. |> in their libraries, so that they can accept programs which define main()
  170. |> but not WinMain().
  171.  
  172. And what will they do with all of the other Windows requests in the
  173. code?  If you are writing Windows code, your program is, by
  174. definition, unportable, and I wouldn't worry about not having a main.
  175. If you are writing code which just happens to run in a shell under
  176. Windows, but is not a Windows program itself, then don't request
  177. Windows support of the compiler, but use the compatible (hosted) mode.
  178. (If the Microsoft compiler doesn't have such a mode, which I seriously
  179. doubt, other Windows compilers do.  Or at least, my old Zortech
  180. compiler did.)
  181.  
  182. --
  183. James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
  184. GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
  185. Conseils, Θtudes et rΘalisations en logiciel orientΘ objet --
  186.                 -- A la recherche d'une activitΘ dans une region francophone
  187.  
  188.  
  189. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  190.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  191.   summarized in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  192. ]
  193.